﻿using UnityEngine;
using System.Collections;
using System;

/// <summary>
/// Game screen.
/// </summary>
using WorldDefine;
using HLLib;
//using HLLIB_ASSETBUNDLE;

public class GameScreen
{

	//	public const int 

	public static GameScreen gSharedGameScreen = null;

	/// <summary>
	/// The KGAM e_ SCREE n_ HTYP.
	/// </summary>
	public const int KGAME_SCREEN_HTYPE = 0;
	public const int KGAME_SCREEN_VTYPE = 1;
	public System.Object curSubScreen = null;
	public GameObject _GlobalObj = null;
//	public LoadingScreen _LoadingScreen = null;


	/// EasyTouch:
//	public static EasyTouch __Easytouch = null;
	public static GameObject __uiroot = null;
	public static Camera __main_camera = null;
	public static Camera _ui_camera = null;



	/// <summary>
	/// The GAM e_ LOADIN.
	/// </summary>
	public const int GAME_LOADING = -1;
	public const int GAME_INIT = 0;
	public const int GAME_RUN = 1;


	//GameManager:
	public  //
	//
	int
	curState,
	oldState,
	curScreenType,
	nextScreenType,
	nextState;
	public static float SCREEN_WIDTH, SCREEN_HEIGHT;
	public static bool isInitGameRes;
	public int gameTimes = 0;
	public int loadingProcess = 0;
	/// <summary>
	/// The is open asset bunlde.
	/// </summary>
	public static bool isOpenAssetBunlde = false;
	public static bool isLoadAssetBundleFromStreamingAssets; // true is StreamingAssets / false is Presidentdatapath
	public static string curAssetBundleSceneName = string.Empty;
	public static string curAssetBundleConfigName = string.Empty;
	public static string curSceneName = string.Empty;
	public static string curAddScriptName = string.Empty;

	//	Music:
	public bool //
	//  
	m_bBackSound,
	m_bEffectsound,
	m_bPaid;




	/// <summary>
	/// 
	/// </summary>
	public GameScreen ()
	{
		Debug.Log ("GameScreen constructor");
		GameObject _GlobalObj = null;
		init ();
	}

	~GameScreen ()
	{

	}

	public void init ()
	{
		curState = -1;
		oldState = -1;
		curScreenType = 0;
		nextScreenType = ( int ) ScreenType.SCREEN_TYPE_LEVEL001; // ( int ) ScreenType.SCREEN_TYPE_LEVEL001; //SCREEN_TYPE_LOGO//SCREEN_TYPE_LEVEL001
		nextState = 0;
		isInitGameRes = false;
		// sound:
		m_bBackSound = true;
		m_bEffectsound = true;
		//
		SCREEN_WIDTH = Screen.width;
		SCREEN_HEIGHT = Screen.height;
		Debug.Log ("SCREEN_WIDTH:  " + SCREEN_WIDTH + ",SCREEN_HEIGHT:  " + SCREEN_HEIGHT);
		/// scrpt globa:
		if(null == _GlobalObj)
			_GlobalObj = GameObject.Find ("_Global");
		///

		createScreen (true);
	}


	/// <summary>
	/// Sets the app init component.
	/// </summary>
	//	public void setAppInitComponent(EasyTouch _Easytouch, UIRoot _uiroot, Camera _main_camera)
	//	{
	//		__Easytouch = _Easytouch;
	//		__uiroot = _uiroot;
	//		__main_camera = _main_camera;
	//
	//	}


	//	/// <summary>
	//	/// Sets the easytouch.
	//	/// </summary>
	//	/// <param name="_et">_et.</param>
	//	public void setEasytouch(EasyTouch _et)
	//	{
	//		__Easytouch = _et;
	//	}
	//
	//
	//	/// <summary>
	//	/// Sets the user interface root.
	//	/// </summary>
	//	public void setUIRoot(UIRoot _uir)
	//	{
	//		__uiroot = _uir;
	//	}


	/// <summary>
	/// Application.targetFrameRate normal -1.
	/// Sets the game frame rote.
	/// Project Settings --> Quality --> Change V sync Count (Don't sync) frameCount start.
	/// </summary>
	public void setGameFrameRote (int frameCount)
	{
		Application.targetFrameRate = frameCount;
	}


	#region ------------------------------ChangeSubScreen-----------------------------
	/// <summary>
	/// Creates the screen.
	/// </summary>
	/// <param name="isFirst">If set to <c>true</c> is first.</param>
	public void createScreen (bool isFirst)
	{

		if (nextState != curState) {
			oldState = curState;
			curState = nextState;
		}


		if (nextScreenType != curScreenType) {
			curState = 0;
			///
			loadingScreen();
			///
		}
		///
		if (isFirst) {
			//			GameObject.Destroy(_GlobalObj.GetComponent<AppInit>());
		}
	}


	/// <summary>
	/// Loadings the screen.
	/// </summary>
	public void loadingScreen()
	{
		loadLevelConfig ();
		this.loadSence ("_scene_loading");
//		_LoadingScreen = _GlobalObj.AddComponent<LoadingScreen>();
	}


	/// <summary>
	/// Loads the level config.
	/// </summary>
	public void loadLevelConfig ()
	{
		//
		switch (nextScreenType) {
		case (int )ScreenType.SCREEN_TYPE_LOGO: /// 
			{
				Debug.Log("SCREEN_TYPE_LOGO: " + nextScreenType);
				curSceneName = "ScreenLogo"; //without assetbundle
				//
				curAssetBundleSceneName = "Scenes/ScreenLogo";/// scene name
				curAssetBundleConfigName = "Configdatas/Level001ConfigData"; /// assetbunle bytes in level.
				///
				curAddScriptName = "ScreenLogo";
				break;
			}
		case (int )ScreenType.SCREEN_TYPE_COVER: /// 
			{   
				Debug.Log("SCREEN_TYPE_COVER: " + nextScreenType);
				curSceneName = "ScreenCover";
				///
				curAssetBundleSceneName = "Scenes/ScreenCover";/// scene name
				curAssetBundleConfigName = string.Empty; /// assetbunle bytes in level
				///
				curAddScriptName = "ScreenCover";
				break;
			}
		case (int )ScreenType.SCREEN_TYPE_SELECT:
			{
				Debug.Log("SCREEN_TYPE_SELECT: " + nextScreenType);
				curSceneName = "ScreenSelect";
				///
				curAssetBundleSceneName = "Scenes/ScreenSelect";/// scene name
				curAssetBundleConfigName = "Configdatas/ScreenSelectConfigData"; /// assetbunle bytes in level
				///
				curAddScriptName = "ScreenSelect";
				break;
			}
		case (int)ScreenType.SCREEN_TYPE_PLATFORM: /// 
			{   
				Debug.Log("SCREEN_TYPE_PLATFORM: " + nextScreenType);
				curSceneName = "ScreenPlatform";
				///
				curAssetBundleSceneName = "Scenes/ScreenPlatform";/// scene name
				curAssetBundleConfigName = string.Empty; /// assetbunle bytes in level
				///
				curAddScriptName = "ScreenPlatform";
				break;
			}
		case (int)ScreenType.SCREEN_TYPE_LEVEL001:
			{
				Debug.Log("SCREEN_TYPE_MIAN_LEVEL001: " + nextScreenType);
				curSceneName = "level001";
				//
				curAssetBundleSceneName = "Scenes/level001";/// scene name
				curAssetBundleConfigName = "Configdatas/Level001ConfigData"; /// assetbunle bytes in level.
				//				        EasyTouch _easytouch = AppInit.getInstance()._easytouch;
				//				        _easytouch.gameObject.SetActive(true);
				curAddScriptName = "ScreenMain";
				//						
				break;
			}
			// ...
		}

	}


	/// <summary>
	/// Changes the state.
	/// </summary>
	/// <param name="_nextState">_next state.</param>
	public void changeState (int _nextState)
	{
		if (nextState != curState) {
			oldState = curState;
			curState = nextState;
		}
	}


	/// <summary>
	/// Changes the sub screen without trans.
	/// </summary>
	/// <param name="_newSubScreenType">_new sub screen type.</param>
	public void changeSubScreenWithoutTrans (int _newSubScreenType)
	{
		changeSubScreenWithoutTrans (_newSubScreenType, 0);
	}


	/// <summary>
	/// Changes the sub screen without trans.
	/// </summary>
	/// <param name="_newSubScreenType">_new sub screen type.</param>
	/// <param name="_nextState">_next state.</param>
	public void changeSubScreenWithoutTrans (int _newSubScreenType, int _nextState)
	{ 
		nextScreenType = _newSubScreenType;
		nextState = _nextState;
		createScreen (curState == -1 ? true : false);
	}


	/// <summary>
	/// Changes the sub screen.
	/// </summary>
	/// <param name="_newSubScreenType">_new sub screen type.</param>
	public void changeSubScreen (int _newSubScreenType)
	{
		changeSubScreen (_newSubScreenType, 0);
	}


	/// <summary>
	/// Changes the sub screen.
	/// </summary>
	/// <param name="_newSubScreenType">_new sub screen type.</param>
	/// <param name="_nextState">_next state.</param>
	public void changeSubScreen (int _newSubScreenType, int _nextState)
	{
		changeSubScreenWithoutTrans (_newSubScreenType, _nextState);
	}


	/// <summary>
	/// Changes the sub screen state without trans.
	/// </summary>
	/// <param name="_newSubScreenState">_new sub screen state.</param>
	public void changeSubScreenStateWithoutTrans (int _newSubScreenState)
	{
		nextState = _newSubScreenState;
		changeState (nextState);
	}


	/// <summary>
	/// Changes the state of the sub screen.
	/// </summary>
	/// <param name="_newSubScreenState">_new sub screen state.</param>
	public void changeSubScreenState (int _newSubScreenState)
	{
		changeSubScreenStateWithoutTrans (_newSubScreenState);
	}


	/// <summary>
	/// Loads the sence.
	/// </summary>
	/// <param name="_sence_name">_sence_name.</param>
	public void loadSence (string _sence_name)
	{
		///
		Application.LoadLevel (_sence_name);
		//        AsyncOperation asyncs = Application.LoadLevelAsync(_sence_name);
		Debug.Log ("loadSence: " + _sence_name);
		///
	}


	/// <summary>
	/// Sets the current screen.
	/// </summary>
	/// <param name="_curScreen">_cur screen.</param>
	public void setCurScreen (System.Object _curScreen)
	{
		curSubScreen = _curScreen;
	}
	#endregion


	/// <summary>
	/// Shareds the game screen manager.
	/// </summary>
	/// <returns>The game screen manager.</returns>
	public static GameScreen SharedGameScreenManager ()
	{
		if (null == gSharedGameScreen) {
			gSharedGameScreen = new GameScreen ();
			return gSharedGameScreen;
		} 
		return gSharedGameScreen;
	}


	/// <summary>
	/// Descriptions the system info.
	/// </summary>
	public void descriptionSystemInfo ()
	{
		//获取参数信息
		string systemInfoLabel = "\n CPU型号：" + SystemInfo.processorType 
			+ "( " + SystemInfo.processorCount 
			+ " cores核心数, " 
			+ SystemInfo.systemMemorySize 
			+ "MB RAM内存)" 
			+ "\n 显卡型号：" 
			+ SystemInfo.graphicsDeviceName
			+ "," 
			+ Screen.width 
			+ "*" 
			+ Screen.height 
			+ " @" 
			+ Screen.currentResolution.refreshRate 
			+ " (" + SystemInfo.graphicsMemorySize + "MB VRAM显存)";
		Debug.Log ("systemInfoLabel: " + systemInfoLabel);
	}

}	
